package com.lifeverse.repository;

import com.lifeverse.entity.Decision;
import com.lifeverse.entity.LifeEntity;
import com.lifeverse.entity.enums.DecisionStatus;
import com.lifeverse.entity.enums.DecisionType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 决策数据访问接口
 */
@Repository
public interface DecisionRepository extends JpaRepository<Decision, Long> {
    
    /**
     * 根据生命体查找决策
     */
    List<Decision> findByLifeEntity(LifeEntity lifeEntity);
    
    /**
     * 根据生命体和状态查找决策
     */
    List<Decision> findByLifeEntityAndStatus(LifeEntity lifeEntity, DecisionStatus status);
    
    /**
     * 根据生命体和类型查找决策
     */
    List<Decision> findByLifeEntityAndType(LifeEntity lifeEntity, DecisionType type);
    
    /**
     * 根据状态查找决策
     */
    List<Decision> findByStatus(DecisionStatus status);
    
    /**
     * 根据类型查找决策
     */
    List<Decision> findByType(DecisionType type);
    
    /**
     * 查找活跃的决策
     */
    @Query("SELECT d FROM Decision d WHERE d.status IN :activeStatuses")
    List<Decision> findActiveDecisions(@Param("activeStatuses") List<DecisionStatus> activeStatuses);
    
    /**
     * 查找超时的决策
     */
    @Query("SELECT d FROM Decision d WHERE d.timeoutAt IS NOT NULL AND d.timeoutAt < :now AND d.status NOT IN :completedStatuses")
    List<Decision> findTimeoutDecisions(@Param("now") LocalDateTime now, 
                                      @Param("completedStatuses") List<DecisionStatus> completedStatuses);
    
    /**
     * 根据生命体分页查找决策
     */
    Page<Decision> findByLifeEntity(LifeEntity lifeEntity, Pageable pageable);
    
    /**
     * 根据生命体和状态分页查找决策
     */
    Page<Decision> findByLifeEntityAndStatus(LifeEntity lifeEntity, DecisionStatus status, Pageable pageable);
    
    /**
     * 查找最近的决策
     */
    @Query("SELECT d FROM Decision d WHERE d.lifeEntity = :lifeEntity ORDER BY d.createdAt DESC")
    List<Decision> findRecentDecisions(@Param("lifeEntity") LifeEntity lifeEntity, Pageable pageable);
    
    /**
     * 统计生命体的决策数量
     */
    long countByLifeEntity(LifeEntity lifeEntity);
    
    /**
     * 统计生命体特定状态的决策数量
     */
    long countByLifeEntityAndStatus(LifeEntity lifeEntity, DecisionStatus status);
    
    /**
     * 统计生命体特定类型的决策数量
     */
    long countByLifeEntityAndType(LifeEntity lifeEntity, DecisionType type);
    
    /**
     * 查找需要重新评估的决策
     */
    @Query("SELECT d FROM Decision d WHERE d.status = 'NEEDS_REEVALUATION' OR " +
           "(d.status = 'EXECUTING' AND d.executedAt < :threshold)")
    List<Decision> findDecisionsNeedingReevaluation(@Param("threshold") LocalDateTime threshold);
    
    /**
     * 根据置信度范围查找决策
     */
    @Query("SELECT d FROM Decision d WHERE d.confidenceScore BETWEEN :minConfidence AND :maxConfidence")
    List<Decision> findByConfidenceRange(@Param("minConfidence") java.math.BigDecimal minConfidence,
                                       @Param("maxConfidence") java.math.BigDecimal maxConfidence);
    
    /**
     * 查找高影响决策
     */
    @Query("SELECT d FROM Decision d WHERE ABS(d.expectedImpact) > :impactThreshold OR ABS(d.actualImpact) > :impactThreshold")
    List<Decision> findHighImpactDecisions(@Param("impactThreshold") java.math.BigDecimal impactThreshold);
    
    /**
     * 查找决策准确性较低的决策
     */
    @Query("SELECT d FROM Decision d WHERE d.expectedImpact IS NOT NULL AND d.actualImpact IS NOT NULL " +
           "AND ABS(d.expectedImpact - d.actualImpact) > :accuracyThreshold")
    List<Decision> findLowAccuracyDecisions(@Param("accuracyThreshold") java.math.BigDecimal accuracyThreshold);
}